import { defineStore } from "pinia";
// import { printStatusApi } from "@/api/xxx";
// export const WS_URL = 'ws://127.0.0.1:13528'
import { ElMessageBox } from "element-plus";

export const usePrintStore = defineStore("print", {
  state: () => ({
    ws: null, // 全局存储一个 websoket ，避免重复创建
    isConnected: false, // 判断菜鸟组件是否以及启动
    callbackEventObj: {},
    isShowErrBox: false,
  }),
  actions: {
    connect() {
      this.ws = new WebSocket("ws://127.0.0.1:13528");
      this.ws.onopen = () => {
        console.log("WebSocket connected");
        this.isConnected = true;
      };
      this.ws.onmessage = (event) => {
        console.log("WebSocket message received:", event.data);
        // 处理收到的消息
        this.subscribe(JSON.parse(event.data || ""));
      };
      this.ws.onclose = () => {
        console.log("WebSocket disconnected");
        this.isConnected = false;
      };
      this.ws.onerror = (error) => {
        console.error("WebSocket error:", error);
      };
    },
    send(message) {
      if (this.isConnected) {
        this.ws.send(message);
      } else {
        console.warn("WebSocket is not connected");
      }
    },
    getUUID(len, radix) {
      var chars =
        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
          ""
        );
      var uuid = [],
        i;
      radix = radix || chars.length;
      if (len) {
        for (var i = 0; i < len; i++)
          uuid[i] = chars[0 | (Math.random() * radix)];
      } else {
        var r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
        uuid[14] = "4";
        for (var i = 0; i < 36; i++) {
          if (!uuid[i]) {
            r = 0 | (Math.random() * 16);
            uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
          }
        }
      }
      return uuid.join("");
    },
    close() {
      if (this.ws) {
        this.ws.close();
        this.ws = null;
      }
    },
    subscribe(data, callBackFn) {
      switch (data.cmd) {
        case "getPrinters":
          console.log("获取打印机列表", data.printers);
          if (data.printers) {
            data.printers = data.printers.filter((i) => {
              return i.status == "enable";
            });
          }
          // 缓存起来
          localStorage.setItem("PRINTERS", JSON.stringify(data.printers || []));
          break;
        case "getPrinterConfig":
          console.log("获取打印机配置");
          console.log(data.printer);
          break;
        case "print":
          console.log("发送打印");
          // console.log(data)
          if (data?.status === "failed" && !this.isShowErrBox) {
            this.isShowErrBox = true;
            ElMessageBox({
              title: "Warning",
              message: `${data?.msg}`,
              confirmButtonText: "I know",
              callback: () => {
                this.isShowErrBox = false;
              },
            });
          }
          break;
        case "notifyPrintResult":
          console.log("打印通知----", data);
          if (["printed", "failed"].includes(data.taskStatus)) {
            // 打印成功/失败 回调记录状态Api接口
            this.printStatus(data);
          }
          break;
        case "setPrinterConfig":
          console.log("设置打印机配置");
          console.log(data);
          break;
      }
    },
    // 这里的data是发送给菜鸟组件的数据按文档要求来，一般有requestID，TaskID等，把自定义的拼接进去
    async printStatus(data) {
      // 传参
      let curId = data?.requestID.split("#") && data?.requestID.split("#")[0];
      // await printStatusApi(curId);
      // this.callbackEventObj[markType]();
    },
  },
});
